package com.fita.admin.handler.impl;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.fita.admin.handler.QrcodeHandler;
import com.fita.admin.rao.QiniuRao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author dan
 *
 */
@Repository("qrcodeHandler")
public class QrcodeHandlerImpl implements QrcodeHandler{
    
    @Value("#{cloud['cloud.domain']}")
    private String qiniuDomain;
    
    private static final String GENERATE_QRCODE_URI = "/generate-qrcode";
    
    private static final Logger logger = LoggerFactory.getLogger(QrcodeHandlerImpl.class);
    
    @Rao
    private QiniuRao qiniuRao;

    /*
     * (non-Javadoc)
     * @see com.fita.admin.handler.QrcodeHandler#generateQrCode(java.lang.String)
     */
    public HttpResult generateQrCode(String url) {
        return generateQrCode(null, url);
    }

    /*
     * (non-Javadoc)
     * @see com.fita.admin.handler.QrcodeHandler#generateQrCode(java.lang.String, java.lang.String)
     */
    public HttpResult generateQrCode(String bucket, String url) {
        HttpResult result = null;

        try {
            
            Map<String, Object> parameters = new HashMap<String, Object>();
            
            parameters.put("url", url);
            if(bucket != null) {
                parameters.put("bucket", bucket);
            }
            
            result = qiniuRao.generateQrcode(qiniuDomain + GENERATE_QRCODE_URI, parameters);
            
            logger.trace("generateQrCode() - Get result key [" + result.getData().get("key").toString() + "].");

        } catch (Exception e) {
            logger.error("generateQrCode() - ", e);
        }

        return result;
    }

}
